<template>
<!-- 用到了 -->

  <div style="width: 100%; height: 100vh; overflow: hidden" :style="bg"> <!--  :style="bg" 加背景图片-->
    <div style="width: 400px; margin: 100px auto">
      <div style="font-size: 30px; text-align: center; padding: 30px 0">GCU调查问卷系统</div>
      <el-form ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="userAccount">
          <el-input prefix-icon="el-icon-user-solid" v-model="form.userAccount" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input prefix-icon="el-icon-lock" v-model="form.passWord" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
         <div style="display: flex">
           <el-input prefix-icon="el-icon-key" v-model="form.validCode" style="width: 50%;" placeholder="请输入验证码"></el-input>
           <ValidCode @input="createValidCode" />
         </div>
        </el-form-item>
        <el-form-item>
          <el-radio v-model="form.role" :label="1">管理员</el-radio>
          <el-radio v-model="form.role" :label="2">普通用户</el-radio>
          <!-- <el-radio v-model="form.role" :label="3" >超级管理员</el-radio> -->
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" @click="login">登 录</el-button>
        </el-form-item>
        <el-form-item><el-button type="text" @click="$router.push('/register')">前往注册 >> </el-button></el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import request from "@/utils/request";
import ValidCode from "@/components/ValidCode";

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    return {
      form: {role: 1},
      rules: {
        userAccount: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        passWord: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      },
      validCode: '',
      // 加背景图片
      bg: {
        backgroundImage: "url(" + require("@/assets/33.jpg") + ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%"
      }
     
    }
  },
  created() {
    sessionStorage.removeItem("user")
  },
  methods: {
    // 接收验证码组件提交的 4位验证码
    createValidCode(data) {
      this.validCode = data
    },
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.form.validCode) {
            this.$message.error("请填写验证码")
            return
          }
          if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            this.$message.error("验证码错误")
            return
          }
          console.log(55555)
          const _this=this
          axios({
               method:"get",
              // url:"http://uchiha.vip:5000/userAccount/?userAccount="+this.form.userAccount+"&passWord="+this.form.passWord,
              url:"/userAccount/?userAccount="+this.form.userAccount+"&passWord="+this.form.passWord,
              headers:
              {
              
              }
          }).then(function (resp){
              console.log(resp)
              console.log(_this.form.userAccount)
              console.log(resp.data.msg)
               if(resp.data.msg=="数据库查询不到账户信息"){
                    _this.$message({
                    type: 'info',
                    message: '用户名或者密码错误'
                  });  
                  return;     
              }
              else {
                  _this.$message({
                    type: "success",
                    message: "登录成功"
                  })
                  _this.$router.push("/book")  
                  sessionStorage.setItem("useraccount",_this.form.userAccount)
                  sessionStorage.setItem("passWord",_this.form.passWord)
                  sessionStorage.setItem('role',resp.data.data)
                  console.log(sessionStorage.getItem('role'))
              }
          }
          )
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
